package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.wemedia.pojos.WmNews;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author: JW
 * @Description: TODO 监听Kafka消息（自媒体端）
 * @DateTime: 2022-02-17 20:55
 **/

@Component
public class ArticleIsDownListener {


    @Autowired
    private ApArticleConfigService apArticleConfigService;

    /**
     * @Author: JW
     * @Description:  TODO 监听Kafka消息（文章上下架） 并修改文章配置表
     * @Date: 20:57 2022-02-17
     * @Param: []
     * @return: void
    **/
    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void upOrDown(String message){

        //取出数据
        if (message != null){
            //解析结果
            Map map = JSON.parseObject(message, Map.class);

            //取出数据
            Long articleId = (Long) map.get("articleId");
            Integer enable = (Integer) map.get("enable");

            //因为数据表设计原因 这里的enable与传过来的数据表示含义不一样
            boolean isDown = enable != 1;

            apArticleConfigService.update(Wrappers.lambdaUpdate(ApArticleConfig.class)
                    .eq(ApArticleConfig::getArticleId,articleId)
                    .set(ApArticleConfig::getIsDown,isDown));

        }
    }

}
